package com.hyt.it.ogt.kq.service.bm.service.impl;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.stereotype.Service;

import com.hyt.core.base.BaseEntity;
import com.hyt.core.base.BaseServiceImpl;
import com.hyt.it.ogt.kq.service.bm.mapper.ProjectStatisticMapper;
import com.hyt.it.ogt.kq.service.bm.model.entity.ProjectStatistic;
import com.hyt.it.ogt.kq.service.bm.service.IProjectStatisticService;

import cn.hutool.core.date.StopWatch;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @program: it-ogt-bm
 * @description: 报名统计
 * @author: Yang HuanKun
 * @create: 2022-09-16 15:05
 **/
@Slf4j
@Service
public class ProjectStatisticService extends BaseServiceImpl<ProjectStatisticMapper, ProjectStatistic>
        implements IProjectStatisticService {
    
    @Override
    public Optional<ProjectStatistic> getProjectStatistic(String projectId, Set<String> deptIds, LocalDateTime createDate) {
        return getBaseMapper().getProjectStatistic(projectId, deptIds, createDate);
    }

    @Override
    public LocalDateTime getLatestCreateDate(String projectId) {
        return this.lambdaQuery()
                .eq(BaseEntity::getDelFlag, Boolean.FALSE)
                .eq(ProjectStatistic::getProjectId, projectId)
                .orderByDesc(BaseEntity::getCreateDate)
                .last("limit 1")
                .oneOpt()
                .map(BaseEntity::getCreateDate)
                .orElseGet(LocalDateTime::now);
    }

    @Override
    public void deleteProjectStatistic() {
        StopWatch stopWatch = StopWatch.create("删除不需要的报名统计数据");

        stopWatch.start("获取累计统计数据大于3次的报名");
        // 获取累计统计数据大于3次的报名
        List<ProjectStatistic> projectStatisticsGtThree = getBaseMapper().listAllProjectStatisticGtThree();
        stopWatch.stop();

        projectStatisticsGtThree.stream()
                // 获取报名ID
                .map(ProjectStatistic::getProjectId)
                // 过滤掉空的报名ID
                .filter(StrUtil::isNotBlank)
                .forEach(projectId -> {
                    stopWatch.start("操作报名：" + projectId);
                    this.lambdaQuery()
                            .select(BaseEntity::getCreateDate)
                            .eq(ProjectStatistic::getDelFlag, Boolean.FALSE)
                            .eq(ProjectStatistic::getProjectId, projectId)
                            .groupBy(BaseEntity::getCreateDate)
                            .orderByDesc(BaseEntity::getCreateDate)
                            .last("limit 3")
                            .list()
                            .stream()
                            // 获取创建时间
                            .map(BaseEntity::getCreateDate)
                            // 过滤掉空的创建时间
                            .filter(ObjectUtil::isNotNull)
                            // 将报名时间正序排序
                            .sorted()
                            // 获取第一个最小的时间
                            .findFirst()
                            .ifPresent(createDate -> {
                                // 将小于最小时间的全部删除
                                this.lambdaUpdate()
                                        .eq(ProjectStatistic::getProjectId, projectId)
                                        .lt(BaseEntity::getCreateDate, createDate)
                                        .remove();
                            });
                    stopWatch.stop();
                });
        log.debug(stopWatch.prettyPrint(TimeUnit.MILLISECONDS));
    }
}
